package base.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import base.model.User;
import base.service.UserService;
import common.utils.Contants;

@Controller
public class LoginController {
	@Autowired
	private UserService userService;
	
	/**
	 * 登录页
	 */
	@RequestMapping(value = "/")
	public String goLogin() {
		return "/login";
	}

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login")
	public String login(User u,HttpServletRequest request) {
		User user = userService.selectByUsername(u.getUsername());
		if(user == null){
			return "/login";
		}else if(user.getPassword().equals(u.getPassword())){
			request.getSession().setAttribute(Contants.LOGINUSER, user);
			return "redirect:/index";
		}else{
			return "/login";
		}
	}
	
	/**
	 * 管理主页
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {
		User user = (User)request.getSession().getAttribute(Contants.LOGINUSER);
		request.setAttribute("user", user);
		return "/index";
	}
	
	/**
	 * 欢迎页
	 */
	@RequestMapping(value = "/welcome")
	public String welcome(HttpServletRequest request) {
		User user = (User)request.getSession().getAttribute(Contants.LOGINUSER);
		request.setAttribute("user", user);
		return "/welcome";
	}
}